package qlsl.androiddesign.view.subview.commonview;

import java.util.Calendar;
import java.util.Date;

import com.qlsl.androiddesign.kuaiwa.R;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import qlsl.androiddesign.activity.commonactivity.MainActivity;
import qlsl.androiddesign.activity.subactivity.AssessmentResultActivity;
import qlsl.androiddesign.activity.subactivity.BrandActivity;
import qlsl.androiddesign.activity.subactivity.CarCategoryActivity;
import qlsl.androiddesign.activity.subactivity.CarTypeActivity;
import qlsl.androiddesign.activity.subactivity.ModelActivity;
import qlsl.androiddesign.entity.commonentity.FragmentArgs;
import qlsl.androiddesign.entity.otherentity.CarBrand;
import qlsl.androiddesign.entity.otherentity.CarModel;
import qlsl.androiddesign.fragment.commonfragment.FragmentTab4;
import qlsl.androiddesign.library.citypicker.CityPopupWindow;
import qlsl.androiddesign.library.citypicker.CityPopupWindow.OnCitySelectListener;
import qlsl.androiddesign.library.datepicker.TimePopupWindow;
import qlsl.androiddesign.library.datepicker.TimePopupWindow.OnTimeSelectListener;
import qlsl.androiddesign.library.datepicker.TimePopupWindow.Type;
import qlsl.androiddesign.method.AppMethod;
import qlsl.androiddesign.util.runfeng.TimeUtil;
import qlsl.androiddesign.view.baseview.FunctionView;

public class TabView4 extends FunctionView<MainActivity> implements
		OnCitySelectListener, OnTimeSelectListener {

	private CityPopupWindow cityWindow;

	private TimePopupWindow timeWindow;

	private TextView tv_category;

	private CarBrand brand;

	private CarModel item;

	public TabView4(FragmentTab4 fragment, ViewGroup rootView,
			ViewGroup contentView) {
		super((MainActivity) fragment.getActivity());
		setContentView(rootView, contentView);
	}

	protected void initView(View view) {
		setTitle("二手车评估");
		setRightButtonResource(R.drawable.personal_h);
		showRightButton();
		cityWindow = new CityPopupWindow(activity, this);
		timeWindow = new TimePopupWindow(activity, Type.YEAR_MONTH_DAY);

	}

	protected void initData() {

	}

	protected void initListener() {
		timeWindow.setOnTimeSelectListener(this);
	}

	public <T> void showData(T... t) {

	}

	public <T> void showNoData(T... t) {

	}

	public FragmentArgs getSerializable() {
		FragmentArgs fragmentArgs2 = new FragmentArgs();

		fragmentArgs2.add("tv_brands", tv_brands);
		fragmentArgs2.add("tv_categorys", tv_categorys);
		fragmentArgs2.add("tv_addresss", tv_addresss);
		fragmentArgs2.add("tv_dates", tv_dates);
		fragmentArgs2.add("forget_pwd_et_validate_codes",
				forget_pwd_et_validate_codes);

		fragmentArgs2.add("postion", postion);
		fragmentArgs2.add("newprice", item);

		Calendar c = Calendar.getInstance();

		fragmentArgs2.add("useyear",
				c.get(Calendar.YEAR) - Integer.parseInt(tv_dates));
		return fragmentArgs2;
	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.email_sign_in_button:
			boolean is = check();
			if (is) {
				startActivity(getSerializable(), AssessmentResultActivity.class);
			} else {
				showToast("请填写");
			}
			break;
		case R.id.tv_brand:
			doClickBrandView();
			break;
		case R.id.tv_category:
			doClickCategoryView();
			break;
		case R.id.tv_date:
			doClickDateView();
			break;
		case R.id.tv_address:
			doClickAddressView();
			break;
		}

	}

	String tv_brands;
	String tv_categorys;
	String tv_addresss;
	String tv_dates;
	String forget_pwd_et_validate_codes;
	int postion;

	private boolean check() {
		TextView tv_brand = (TextView) findViewById(R.id.tv_brand);
		TextView tv_category = (TextView) findViewById(R.id.tv_category);
		TextView tv_date = (TextView) findViewById(R.id.tv_date);
		TextView tv_address = (TextView) findViewById(R.id.tv_address);

		EditText forget_pwd_et_validate_code = (EditText) findViewById(R.id.forget_pwd_et_validate_code);

		tv_brands = tv_brand.getText().toString();
		tv_categorys = tv_category.getText().toString();
		tv_addresss = tv_address.getText().toString();
		tv_dates = tv_date.getText().toString();
		forget_pwd_et_validate_codes = forget_pwd_et_validate_code.getText()
				.toString();
		postion = ((RadioButton) findViewById(R.id.btn_no_1)).isChecked() ? 1
				: 2;// 1为否 2为是

		if (TextUtils.isEmpty(tv_brands)) {
			return false;
		}
		if (TextUtils.isEmpty(tv_categorys)) {
			return false;
		}
		if (TextUtils.isEmpty(tv_addresss)) {
			return false;
		}
		if (TextUtils.isEmpty(tv_dates)) {
			return false;
		}

		if (TextUtils.isEmpty(forget_pwd_et_validate_codes)) {
			return false;
		}

		return true;
	}

	private void doClickBrandView() {

		startActivityForResult(BrandActivity.class, 0);

		// startActivityForResult(CarTypeActivity.class, 0);
	}

	private void doClickCategoryView() {
		TextView tv_brand = (TextView) findViewById(R.id.tv_brand);
		String title = tv_brand.getText().toString();
		if (TextUtils.isEmpty(title) || title.contains("请选择")) {
			showToast("请选择品牌");
			return;
		}
		// Intent intent = new Intent(activity, CarCategoryActivity.class);
		// intent.putExtra("title", title);
		// startActivityForResult(intent, 1);

		Intent intent = new Intent(activity, ModelActivity.class);
		intent.putExtra("brandId", brand.getId());
		startActivityForResult(intent, 1);
	}

	private void doClickDateView() {
		timeWindow.showAtLocation(view, Gravity.BOTTOM
				| Gravity.CENTER_HORIZONTAL, 0, 0);
	}

	private void doClickAddressView() {
		cityWindow.showAtLocation(view, Gravity.BOTTOM
				| Gravity.CENTER_HORIZONTAL, 0, 0);
	}

	public void onTimeSelect(Date date) {
		String time = AppMethod.formatDate(date, '-').split("-")[0];
		TextView tv_date = findViewById(R.id.tv_date);
		tv_date.setText(time);
	}

	public void onCitySelect(String city_) {
		String province = cityWindow.getCityPicker().getProvince();
		String city = cityWindow.getCityPicker().getCity();
		TextView tv_address = findViewById(R.id.tv_address);
		tv_address.setText(province + city);
	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 0) {
			if (data == null) {
				return;
			}
			// String text = data.getStringExtra("text");

			brand = (CarBrand) data.getSerializableExtra("brand");

			TextView tv_brand = ((TextView) findViewById(R.id.tv_brand));
			tv_brand.setText(brand.getName());
		} else if (requestCode == 1) {
			if (data == null) {
				return;
			}
			item = (CarModel) data.getExtras().get("item");

			tv_category = ((TextView) findViewById(R.id.tv_category));

			tv_category.setText(item.getName());
		}
	};
}
